<template>
  <div class="login">
    <div class="left">
      <div class="header">
        <img class="img" src="@/assets/imgs/图层 8.png" alt />
        <span class="heima">黑马面面</span>
        <div class="line"></div>
        <span class="us">用户登录</span>
      </div>
      <el-form ref="form" :model="form" label-width="0px" class="form" :rules="rules">
        <el-form-item prop="phone">
          <el-input v-model="form.phone" placeholder="请输入您的手机号" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入您的密码"
            prefix-icon="el-icon-lock"
            show-password
          ></el-input>
        </el-form-item>

        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input
                placeholder="请输入验证码"
                v-model="form.code"
                style="width: 100%;"
                prefix-icon="el-icon-key"
              ></el-input>
            </el-col>
            <el-col :span="8">
              <img @click="refreshCode" :src="codeUrl" class="code-pic" />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass" :label="true">
            我已阅读并同意
            <el-link type="primary" class="check">用户协议</el-link>和
            <el-link type="primary" class="check">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="login">登录</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="showRegister">注册</el-button>
        </el-form-item>
      </el-form>
    </div>

    <img src="@/assets/imgs/login_banner_ele.png" alt />

    <register ref="register"></register>
  </div>
</template>

<script>
import { saveLocal} from '@/utils/local.js'
import { toLogin } from '@/api/login.js'
import register from './register'
export default {
  components: {
    register,
  },
  data() {
    return {
      codeUrl: process.env.VUE_APP_URL + '/captcha?type=login',
      form: {
        phone: '',
        password: '',
        code: '',
        isPass: [],
      },
      rules: {
        phone: [
          {
            required: true,
            message: '请输入手机号',
            trigger: 'change',
          },
          {
            validator: (rule, value, callback) => {
              const _reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (_reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正确手机号'))
              }
            },
            trigger: 'change',
          },
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'change',
          },
          {
            min: 6,
            max: 12,
            message: '请输入6-12位密码',
            trigger: 'change',
          },
        ],
        code: [
          {
            required: true,
            message: '请输入验证码',
            trigger: 'change',
          },
          {
            min: 4,
            max: 4,
            message: '请输入正确验证码',
            trigger: 'change',
          },
        ],
        isPass: [
          {
            required: true,
            message: '请勾选协议',
            trigger: 'change',
          },
        ],
      },
    }
  },
  methods: {
    login() {
      this.$refs.form.validate((result) => {
        if (result) {
          // 传给toLogin参数的同时调用promiss的.then()方法
          toLogin(this.form).then((res) => {
            this.$message.success('登陆成功')
            saveLocal('token', res.data.data.token)
            // 登录成功后跳转到layout
            this.$router.push('/layout')
            window.console.log('登录返回信息:', res)
          })
        } else {
          this.$message.error('验证失败')
        }
      })
    },
    showRegister() {
      this.$refs.register.isShow = true
    },
    refreshCode() {
      this.codeUrl =
        process.env.VUE_APP_URL + '/captcha?type=login&basdfg' + Date.now()
    },
  },
}
</script>

<style lang="less">
.login {
  height: 100%;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  display: flex;
  align-items: center;
  justify-content: space-around;
}
.left {
  width: 478px;
  height: 550px;
  background: #f5f5f5;
  padding: 44px 42px;
}
.header {
  display: flex;
  align-items: center;
}
.img {
  width: 25px;
}
.heima {
  font-size: 24px;
  font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
  font-weight: 400;
  text-align: left;
  color: #0c0c0c;
  margin: 0 13px;
}
.line {
  width: 1px;
  height: 28px;
  background: #c7c7c7;
}
.us {
  margin-left: 13px;
  font-size: 22px;
  font-family: PingFangSC Regular, PingFangSC Regular-Regular;
  font-weight: 400;
  text-align: left;
  color: #0c0c0c;
}
.form {
  margin-top: 25px;
}
.check {
  vertical-align: top;
}
.code-pic {
  width: 100%;
}
.btn {
  width: 100%;
  border-radius: 4px;
}
</style>
